﻿package {
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;
	import flash.geom.*;
	import flash.system.*;
	public final class ByteArray2String extends MovieClip {

		private var skin_url:String = "default.zip";

		private var skinStream:URLStream;

		private var the_string:String;

		public function ByteArray2String() {
			bt_make.addEventListener(MouseEvent.CLICK, loadZip);
			output.addEventListener(MouseEvent.CLICK, copyString);
		}
		
		private function copyString(e:MouseEvent):void {
			var str:String = output.text;
			output.setSelection(0, str.length);
			System.setClipboard(str);
		}
			
		private function loadZip(e:MouseEvent):void {
			skin_url = input_src.text;
			skinStream = new URLStream();
			skinStream.load(new URLRequest(skin_url));
			skinStream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler,false,0,true);
			skinStream.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler,false,0,true);
			skinStream.addEventListener(Event.COMPLETE,skinDone,false,0,true);
		}

		private function errorHandler(e:Event):void {
			output.text = skin_url + "\n" + e.toString();
		}
		private function skinDone(e:Event):void {
			var ba1:ByteArray = new ByteArray();
			ba1.endian = Endian.LITTLE_ENDIAN;
			skinStream.readBytes(ba1);
			
			//初始字符
			//showByteArray(ba1);

			//转字符串
			var str1:String = byte2string(ba1);
			output.text = str1;

			//解压字符
			///var ba2:ByteArray = string2byte(str1);
			//showByteArray(ba2);
			
		}
		
		private function byte2string(ba:ByteArray):String {
			var val:String = "";
			ba.compress();
			ba.position = 0;
			for (var i:int = 0; i < ba.length; i++) {
				var n:String = ba.readUnsignedByte().toString(16);
				if(n.length < 2){ 
            		n = "0" + n; 
        		}
				val += n;
			}
			var bytes:ByteArray = new ByteArray();
			bytes.writeUTFBytes(val);
			bytes.compress();
			return B64.encode2string(bytes);
		}
		
		private function string2byte(str:String):ByteArray {
			var decode:ByteArray = B64.decode2bytes(str);
			decode.uncompress();
			str = decode.toString();
			var ba:ByteArray = new ByteArray();
			ba.endian = Endian.LITTLE_ENDIAN; 
			for (var i:int = 0; i < str.length; i += 2){  
				var buf:String = "0x" + str.charAt(i) + str.charAt(i + 1);
				ba.writeByte(int(buf));
			}
			ba.uncompress();
			return ba;
		}
		


		
		//=============================================================================
		
		private function outputString(str:String):void {
			var result:String = "";
			for (var i:int = 0; i < str.length; i++) {
				result += str.charCodeAt(i) + ",";
			}
			trace(result);

		}
		
		private function showByteArray(ba:ByteArray):void {
			var tt:String = "";
			ba.position = 0;
			for (var i:int = 0; i < ba.length; i++) {
				var n:String = ba.readUnsignedByte().toString(16);
				if(n.length < 2){ 
            		n = "0" + n; 
        		} 
				tt += n + ",";
			}
			trace(tt);
		}
		
		

	}
}